////////////////////////////////////////////////////////////////////////////////
//
//	This file is part of Woozox, see <http://code.google.com/p/woozox/>.
//
//	Copyright 2008-2009 Florian David
//
//	Woozoo is free software: you can redistribute it and/or modify
//	it under the terms of the GNU Lesser General Public License as 
//	published by the Free Software Foundation, either version 3 of 
//	the License, or (at your option) any later version.
//
//	Woozoo is distributed in the hope that it will be useful,
//	but WITHOUT ANY WARRANTY; without even the implied warranty of
//	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//	GNU General Public License for more details.
//
//	You should have received a copy of the GNU Lesser General Public 
//	License along with Woozoo. If not, see <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
package org.woozoo.xhtml.core.base.selector
{
	import org.woozoo.xhtml.core.base.*;

	/**
	 * @private
	 * */
	internal class SelectorOperatorAttrSomething extends AbstractSelectorOperator
	{
		public var delimiter : String;
		public var elementFunctionName : String;
		public var childrenFunctionName : String;

		private var __att : String;
		private var __value : String;

		override protected function _prepareString(str:String):String {
			var split : Array = str.split(delimiter+'='); 
			var att : String = (split[0] as  String).replace('[', "");
			var value : String = (split[1] as  String).replace(']', "");
			
			var valueLen : int = value.length;
			if (value.charAt(0) == '"' || value.charAt(0) == "'") {
				value = value.slice(1, valueLen);
				valueLen--;
			}
			if (value.charAt(valueLen - 1) == '"' || value.charAt(valueLen - 1) == "'") {
				value = value.slice(0, valueLen - 1);
			}
			__att = att;
			__value = value;

			return str;
		}
		
		override protected function _operateChildrenElements(checkElements : Boolean) : Array {
			debugFunction('we interpret '+delimiter+' '+ str+' :: att=['+__att+'] value=['+__value+']');
			var res : Array = [];
			for (var i : int = 0; i < elementsLength; i++) {
				var element : Element = getElementAt(i);
				if (checkElements && element[elementFunctionName](__att, __value)) {
					res.push(element);
				}
				res = res.concat(element[childrenFunctionName](__att, __value));
			}
			return res;
		}
		
		override protected function _operateElements():Array {
			debugFunction('we interpret '+delimiter+' '+ str+' :: att=['+__att+'] value=['+__value+']');

			var res : Array = [];
			for (var i : int = 0; i < elementsLength; i++) {
				var element : Element = getElementAt(i);
				if (element[elementFunctionName](__att, __value)) {
					res.push(element);
				}
			}
			return res;
		}
	}
}